package com.baizhi.service.impl;


import com.baizhi.dao.ArticleMapper;
import com.baizhi.entity.Article;
import com.baizhi.service.ArticleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Author: renpu
 * @Date: 2019/2/27 19:17
 * @Description:
 */
@Service
@Transactional
public class ArticleServiceImpl implements ArticleService{

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public Map selectArticle(int rows, int page) {

        Map map=new HashMap();

        //创建IPage对象计算出来开始条数和结束的条数

        IPage<Article> iPage=new Page<>(rows,page);

        //传入到selectPage方法进行分页展示数据；

        IPage<Article> articleIPage=articleMapper.selectPage(iPage,null);

        //计算出每页展示的数据；

        List<Article> list=articleIPage.getRecords();

        //获取到总条数；

        long count=articleIPage.getTotal();

        map.put("rows",list);
        map.put("total",count);
        return map;
    }

    @Override
    public void update(Article article) {

        articleMapper.updateById(article);
    }

    @Override
    public void deleteMany(int[] ids) {
        List<Integer> list=new ArrayList<>();

        for (Integer id: ids) {
            list.add(id);
        }
            articleMapper.deleteBatchIds(list);

    }

    @Override
    public void addArticle(Article article) {

        articleMapper.insert(article);
    }
}
